package com.start.avue.mapper;


import com.start.avue.calculate.StartCalculate;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Data
@Schema(name = "AvueColumnSummary", description = "列统计计算")
public class AvueColumnSummary {
    @Schema(name = "数据精度")
    private int decimals;
    @Schema(name = "字段名称", description = "prop")
    private String name;
    @Schema(name = "计算类型", description = "sum 相加,avg 平均,count 统计")
    private String type;
    @Schema(name = "前缀")
    private String label;

    @Schema(name = "计算函数")
    private StartCalculate calculate;
    @Schema(name = "计算值")
    private BigDecimal value;

    /**
     * 重置
     */
    public void rest() {
        if (calculate != null) {
            calculate.rest();
        }
        value = null;
        if (StringUtils.isNotBlank(name)) {
            value = new BigDecimal(0);
        }
    }

    public String getFormatValue() {
        StringBuilder sb = new StringBuilder();
        if (label != null && label.length() > 0) {
            sb.append(label);
        }
        if (value == null) return sb.toString();
        value.setScale(decimals, RoundingMode.DOWN);
        sb.append(value);
        return sb.toString();
    }
}
